package com.example.demo.service.impl;

import com.example.demo.entity.Question;
import com.example.demo.exception.ServiceException;
import com.example.demo.mapper.ICategoryMapper;
import com.example.demo.mapper.IQuestionMapper;
import com.example.demo.param.vo.QuestionArrVO;
import com.example.demo.param.vo.QuestionVO;
import com.example.demo.service.ICateGoryService;
import com.example.demo.service.IQuestionService;
import com.example.demo.util.Judge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired private ICategoryMapper categoryMapper;
    @Autowired private IQuestionMapper questionMapper;

    @Override
    public Boolean insert(String uid, String categroy, String title, String content) throws ServiceException {
        if(Judge.isNull(categroy) || Judge.isNull(title) || Judge.isNull(content)){
            throw new ServiceException("请务必完善必填项");
        }
        if(this.categoryMapper.getById(categroy) == null){
            throw new ServiceException("暂无当前分类");
        }
        Integer affect = this.questionMapper.insert(
                Judge.getUUID(), categroy, title, content,uid,0, 0, new Date());
        if(affect != 1){
            throw new ServiceException("新增失败");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean delete(String id) throws ServiceException {
        QuestionVO questionVO = this.questionMapper.getById(id);
        if(questionVO == null){
            throw new ServiceException("未找到指定内容");
        }
        Question question = questionVO.getQuestion();
        if(question == null){
            throw new ServiceException("未找到指定内容");
        }
        question.setIsDelete(1);
        Integer affect = this.questionMapper.update(
                question.getCategory(), question.getTitle(), question.getContent(),
                question.getIsStatu(), question.getIsDelete(), question.getId(),question.getBrowse());
        if(affect != 1){
            throw new ServiceException("删除失败");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean update(String id, String uid, String title, String content, Integer statu) throws ServiceException {
        if(Judge.isNull(id)){
            throw new ServiceException("请提供有效id");
        }
        QuestionVO questionVO = this.questionMapper.getById(id);
        if(questionVO == null){
            throw new ServiceException("未找到指定内容");
        }
        Question question = questionVO.getQuestion();
        if(question == null){
            throw new ServiceException("未找到指定内容");
        }
        if(!question.getUid().equals(uid)){
            throw new ServiceException("不能修改他人问题");
        }
        if(title != null){
            question.setTitle(title);
        }
        if(content != null){
            question.setContent(content);
        }
        if(statu != null){
            if(statu < 0 || statu > 1){
                throw new ServiceException("错误的解决状态码");
            }
            question.setIsStatu(statu);
        }
        Integer affect = this.questionMapper.update(
                question.getCategory(), question.getTitle(), question.getContent(),
                question.getIsStatu(), question.getIsDelete(), question.getId(),question.getBrowse());
        if(affect != 1){
            throw new ServiceException("修改失败");
        }
        return Boolean.TRUE;
    }

    @Override
    public QuestionVO get(String id) throws ServiceException {
        if(Judge.isNull(id)){
            throw new ServiceException("请提供有效id");
        }
        QuestionVO questionVO = this.questionMapper.getById(id);
        if (questionVO == null){
            return null;
        }
        Question question = questionVO.getQuestion();
        question.setBrowse(question.getBrowse()+1);
        Integer affect = this.questionMapper.update(
                question.getId(), question.getTitle(), question.getContent(),
                question.getIsStatu(), question.getIsDelete(), question.getId(),
                question.getBrowse());
        if (affect != 1){
            throw new ServiceException("浏览失败!");
        };
        return questionVO;
    }

    @Override
    public QuestionArrVO getQueryPage(String category, String title, String content, String uid,
                                         Integer statu, Integer page, Integer pageSize) throws ServiceException {
        if(page == null || page < 1 || pageSize == null || pageSize < 1){
            throw new ServiceException("请正确填写分页");
        }
        QuestionArrVO questionArrVO = this.questionMapper.getQureyPage(
                category, title, content, uid, statu, page, pageSize);
        questionArrVO.setPage(page);
        questionArrVO.setPageSize(pageSize);
        return questionArrVO;
    }
}
